package com.ruoyi.customerSample.service.impl;

import com.ruoyi.common.utils.uuid.IdUtils;

import com.ruoyi.customerSample.domain.Customer;
import com.ruoyi.customerSample.domain.SampleSendDetails;
import com.ruoyi.customerSample.domain.SampleSendInfo;
import com.ruoyi.customerSample.domain.SampleSendInstrument;
import com.ruoyi.customerSample.mapper.CustomerMapper;
import com.ruoyi.customerSample.mapper.SampleSendDetailsMapper;
import com.ruoyi.customerSample.mapper.SampleSendInfoMapper;
import com.ruoyi.customerSample.mapper.SampleSendInstrumentMapper;
import com.ruoyi.customerSample.service.ISampleSendInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * 来样登记信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-05
 */
@Service
public class SampleSendInfoServiceImpl implements ISampleSendInfoService
{
    @Autowired(required = false)
    private SampleSendInfoMapper sampleSendInfoMapper;

    @Autowired(required = false)
    private SampleSendDetailsMapper sampleSendDetailsMapper;

    @Autowired(required = false)
    private SampleSendInstrumentMapper sampleSendInstrumentMapper;

    @Autowired(required = false)
    private CustomerMapper reportCompanyMapper;

    /**
     * 查询来样登记信息
     * 
     * @param id 来样登记信息ID
     * @return 来样登记信息
     */
    @Override
    public SampleSendInfo selectSampleSendInfoById(String id)
    {
        SampleSendInfo SampleSendInfo = sampleSendInfoMapper.selectSampleSendInfoById(id);
        //获取明细详情信息
        SampleSendDetails sampleSendDetails = new SampleSendDetails();
        sampleSendDetails.setSampleId(id);
        List<SampleSendDetails> list = sampleSendDetailsMapper.selectSampleSendDetailsList(sampleSendDetails);
        SampleSendInfo.setList(list);
        //获取类别信息
        SampleSendInstrument sampleSendInstrument = new SampleSendInstrument();
        sampleSendInstrument.setSampleId(id);
        List<SampleSendInstrument> children = sampleSendInstrumentMapper.selectSampleSendInstrumentList(sampleSendInstrument);
        SampleSendInfo.setChildren(children);
        return SampleSendInfo;
    }

    @Override
    public SampleSendInfo selectSampleSendInfoByCompany(String client, String reDate) {
        SampleSendInfo sampleSendInfo = sampleSendInfoMapper.selectSampleSendInfoByCompany(client, reDate);
        if(null != sampleSendInfo) {
            //获取明细详情信息
            SampleSendDetails sampleSendDetails = new SampleSendDetails();
            sampleSendDetails.setSampleId(sampleSendInfo.getId());
            List<SampleSendDetails> list = sampleSendDetailsMapper.selectSampleSendDetailsList(sampleSendDetails);
            sampleSendInfo.setList(list);
            //获取类别信息
            SampleSendInstrument sampleSendInstrument = new SampleSendInstrument();
            sampleSendInstrument.setSampleId(sampleSendInfo.getId());
            List<SampleSendInstrument> children = sampleSendInstrumentMapper.selectSampleSendInstrumentList(sampleSendInstrument);
            sampleSendInfo.setChildren(children);
        }
        return sampleSendInfo;
    }

    /**
     * 获取送样单位的日期数据
     * @param client
     * @return
     */
    @Override
    public List<String> selectReDateByCompany(String client) {
        return sampleSendInfoMapper.selectReDateByCompany(client);
    }

    /**
     * 查询来样登记信息列表
     * 
     * @param sampleSendInfo 来样登记信息
     * @return 来样登记信息
     */
    @Override
    public List<SampleSendInfo> selectSampleSendInfoList(SampleSendInfo sampleSendInfo)
    {
        List<SampleSendInfo> SampleSendInfoList = sampleSendInfoMapper.selectSampleSendInfoList(sampleSendInfo);
        for(SampleSendInfo s : SampleSendInfoList) {
            //获取明细详情信息
            SampleSendDetails sampleSendDetails = new SampleSendDetails();
            sampleSendDetails.setSampleId(s.getId());
            List<SampleSendDetails> list = sampleSendDetailsMapper.selectSampleSendDetailsList(sampleSendDetails);
            s.setList(list);
            //获取类别信息
            SampleSendInstrument sampleSendInstrument = new SampleSendInstrument();
            sampleSendInstrument.setSampleId(s.getId());
            List<SampleSendInstrument> children = sampleSendInstrumentMapper.selectSampleSendInstrumentList(sampleSendInstrument);
            s.setChildren(children);
        }
        return SampleSendInfoList;
    }

    /**
     * 批量导入
     * @param groupingMap
     * @return
     */
    @Override
    public void importSampleSendInfo(Map<List<Object>, List<SampleSendDetails>> groupingMap) {
        List<SampleSendInfo> list = new ArrayList<SampleSendInfo>();
        if(!groupingMap.isEmpty()) {
            for(Map.Entry<List<Object>, List<SampleSendDetails>> entry : groupingMap.entrySet()){
                //插入基本信息内容
                if(!entry.getKey().isEmpty()) {
                    SampleSendInfo sampleSendInfo = new SampleSendInfo();
                    if(null != entry.getKey().get(0)) {
                        sampleSendInfo.setReDate((Date)entry.getKey().get(0));
                    }
                    if(null != entry.getKey().get(1)) {
                        sampleSendInfo.setClient(entry.getKey().get(1).toString());
                    }
                    if(null != entry.getKey().get(2)) {
                        sampleSendInfo.setClientAdress(entry.getKey().get(2).toString());
                    }
                    if(null != entry.getKey().get(3)) {
                        sampleSendInfo.setSampleReceiver(entry.getKey().get(3).toString());
                    }
                    if(null != entry.getKey().get(4)) {
                        sampleSendInfo.setSampleSender(entry.getKey().get(4).toString());
                    }
                    if(null != entry.getKey().get(5)) {
                        sampleSendInfo.setPhonenum(entry.getKey().get(5).toString());
                    }
                    if(!entry.getValue().isEmpty()) {
                        sampleSendInfo.setList(entry.getValue());
                    }
                    insertSampleSendInfo(sampleSendInfo);
                }
            }
        }
        //if(list.size() > 0) {
        //    //获取来样类型信息
        //    for(SampleSendInfo s : list){
        //        List<SampleSendInstrument> children = new ArrayList<SampleSendInstrument>();
        //        SampleSendInstrument sampleSendInstrument = new SampleSendInstrument();
        //        sampleSendInstrument.setSampleType(StringUtils.substring(s.getList().get(0).getSampleNum(), 2, 4));
        //        sampleSendInstrument.setNums(String.valueOf(s.getList().size()));
        //        children.add(sampleSendInstrument);
        //        s.setChildren(children);
        //    }
        //}
    }

    /**
     * 新增来样登记信息
     * 
     * @param sampleSendInfo 来样登记信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSampleSendInfo(SampleSendInfo sampleSendInfo)
    {
        //新增来样明细信息和来样类型信息
        String id = IdUtils.fastSimpleUUID();
        if(null != sampleSendInfo.getList() && sampleSendInfo.getList().size() > 0) {
            for(SampleSendDetails detailBill: sampleSendInfo.getList()) {
                detailBill.setId(IdUtils.fastSimpleUUID());
                detailBill.setSampleId(id);
            }
            sampleSendDetailsMapper.batchSampleSendDetails(sampleSendInfo.getList());
        }
        if(null != sampleSendInfo.getChildren() && sampleSendInfo.getChildren().size() > 0) {
            for(SampleSendInstrument sampleSendInstrument : sampleSendInfo.getChildren()) {
                sampleSendInstrument.setId(IdUtils.fastSimpleUUID());
                sampleSendInstrument.setSampleId(id);
            }
            sampleSendInstrumentMapper.batchSampleSendInstrument(sampleSendInfo.getChildren());
        }
        sampleSendInfo.setId(id);
        int ret = sampleSendInfoMapper.insertSampleSendInfo(sampleSendInfo);
        if(ret > 0) {
            //查询委托单位是否存在，不存在则添加
            Customer c = new Customer();
            c.setCompanyName(sampleSendInfo.getClient());
            c.setAdress(sampleSendInfo.getClientAdress());
            List<Customer> reportCompanyList = reportCompanyMapper.selectCustomerList(c);
            if(null == reportCompanyList ) {
                Customer reportCompany = new Customer();
                reportCompany.setId(IdUtils.fastSimpleUUID());
                reportCompany.setCompanyName(sampleSendInfo.getClient());
                reportCompany.setAdress(sampleSendInfo.getClientAdress());
                reportCompany.setCharger(sampleSendInfo.getSampleSender());
                reportCompany.setChargerPhone(sampleSendInfo.getPhonenum());
                reportCompanyMapper.insertCustomer(reportCompany);
            }
        }
        return ret;
    }

    /**
     * 修改来样登记信息
     * 
     * @param sampleSendInfo 来样登记信息
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateSampleSendInfo(SampleSendInfo sampleSendInfo)
    {
        //先删除来样明细再新增
        int r = sampleSendDetailsMapper.deleteSampleSendDetailsBySampleId(sampleSendInfo.getId());
        if(r > 0) {
            for(SampleSendDetails detailBill: sampleSendInfo.getList()) {
                detailBill.setId(IdUtils.fastSimpleUUID());
                detailBill.setSampleId(sampleSendInfo.getId());
            }
        }
        if(null != sampleSendInfo.getChildren() && sampleSendInfo.getChildren().size() > 0) {
            int i = sampleSendInstrumentMapper.deleteSampleSendInstrumentBySampleId(sampleSendInfo.getId());
            if(i > 0) {
                for(SampleSendInstrument sampleSendInstrument : sampleSendInfo.getChildren()) {
                    sampleSendInstrument.setId(IdUtils.fastSimpleUUID());
                    sampleSendInstrument.setSampleId(sampleSendInfo.getId());
                }
            }
            sampleSendInstrumentMapper.batchSampleSendInstrument(sampleSendInfo.getChildren());
        }
        sampleSendDetailsMapper.batchSampleSendDetails(sampleSendInfo.getList());
        return sampleSendInfoMapper.updateSampleSendInfo(sampleSendInfo);
    }

    /**
     * 批量删除来样登记信息
     * 
     * @param ids 需要删除的来样登记信息ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSampleSendInfoByIds(String[] ids)
    {
        //批量删除明细信息
        for(int i = 0; i < ids.length; i++) {
            //删除明细信息
            sampleSendDetailsMapper.deleteSampleSendDetailsBySampleId(ids[i]);
            sampleSendInstrumentMapper.deleteSampleSendInstrumentBySampleId(ids[i]);
        }
        return sampleSendInfoMapper.deleteSampleSendInfoByIds(ids);
    }

    /**
     * 删除来样登记信息信息
     * 
     * @param id 来样登记信息ID
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteSampleSendInfoById(String id)
    {
        //删除明细信息
        sampleSendDetailsMapper.deleteSampleSendDetailsBySampleId(id);
        sampleSendInstrumentMapper.deleteSampleSendInstrumentBySampleId(id);
        return sampleSendInfoMapper.deleteSampleSendInfoById(id);
    }
}
